package com.gzy.literature.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gzy.literature.dao.UserFavoritesDao;
import com.gzy.literature.entity.UserFavorites;
import com.gzy.literature.service.UserFavoritesService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

@Service("userFavoritesService")
public class UserFavoritesServiceImpl implements UserFavoritesService {

    @Resource
    private UserFavoritesDao userFavoritesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象，若不存在则返回null
     */
    @Override
    public UserFavorites queryById(String userId) {
        if (userId == null || userId.trim().isEmpty()) {
            // throw new IllegalArgumentException("User ID must not be null or empty");
            return null;
        }
        return this.userFavoritesDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param userFavorites 筛选条件，可以为null表示无条件查询
     * @param pageNum 页码，从1开始，必须为正整数
     * @param pageSize 页面大小，必须为正整数
     * @return 查询结果
     */
    @Override
    public PageInfo<UserFavorites> queryDll(UserFavorites userFavorites, int pageNum, int pageSize) {
        if (pageNum <= 0 || pageSize <= 0) {
            throw new IllegalArgumentException("Page number and page size must be positive integers");
        }
        PageHelper.startPage(pageNum, pageSize);

        List<UserFavorites> userFavoritesList = userFavoritesDao. queryDll(userFavorites);

        PageInfo<UserFavorites> pageInfo = new PageInfo<>(userFavoritesList);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param userFavorites 实例对象，必须包含有效的userId和其他必要字段
     * @return 实例对象
     */
    @Override
    public UserFavorites insert(UserFavorites userFavorites) {
        if (userFavorites == null || userFavorites.getUserId() == null || userFavorites.getUserId().trim().isEmpty()) {
            throw new IllegalArgumentException("User Favorites object and user ID must not be null or empty");
        }
        // 这里可以添加对其他必要字段的验证
        this.userFavoritesDao.insert(userFavorites);
        return userFavorites;
    }

    /**
     * 修改数据
     *
     * @param userFavorites 实例对象，必须包含有效的userId
     * @return 修改后的实例对象
     */
    @Override
    public UserFavorites update(UserFavorites userFavorites) {
        if (userFavorites == null || userFavorites.getUserId() == null || userFavorites.getUserId().trim().isEmpty()) {
            throw new IllegalArgumentException("User Favorites object and user ID must not be null or empty");
        }
        this.userFavoritesDao.update(userFavorites);
        // 调用queryById来确保返回的是数据库中的最新数据
        return this.queryById(userFavorites.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键，必须为非空且非空字符串
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String userId) {
        if (userId == null || userId.trim().isEmpty()) {
            throw new IllegalArgumentException("User ID must not be null or empty");
        }
        return this.userFavoritesDao.deleteById(userId) > 0;
    }
}